<?php

/**
 * ECSHOP 搜索程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: sxc_shop $
 * $Id: search.php 16908 2009-12-18 08:25:08Z sxc_shop $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);


require(dirname(__FILE__) . '/includes/init.php');

//$_REQUEST = array_merge($_REQUEST, addslashes_deep($string));

//$_REQUEST['act'] = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : '';
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//
//}

/*------------------------------------------------------ */
//-- 搜索结果
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_search')
{
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :10;
//	$sex =!empty($_REQUEST['sex'])?intval($_REQUEST['sex']) :0;
	$cat =!empty($_REQUEST['c'])?intval($_REQUEST['c']) :0;
//	$child_cat =!empty($_REQUEST['child_cat'])?intval($_REQUEST['child_cat']) :0;            			
//	$attr_id =!empty($_REQUEST['attr_id'])?intval($_REQUEST['attr_id']) :0;            				
	$brand =!empty($_REQUEST['b'])?intval($_REQUEST['b']) :0;     

	/* 商品分类 */
//	$top_id = $cat;
//	/* 商品价格 */
//	$min_price  = -1;
//	$max_price  = -1;						
//	if ($ptype==1){
//		$min_price  = 0;	$max_price  = 300;				
//	}
//	if ($ptype==2){
//		$min_price  = 300;	$max_price  = 400;				
//	}	
//	if ($ptype==3){
//		$min_price  = 400;	$max_price  = 600;				
//	}		
//	if ($ptype==4){
//		$min_price  = 600;	$max_price  = 1000;				
//	}			
//	if ($ptype==5){
//		$min_price  = 1000;	$max_price  = 99999;				
//	}
		
    /* 相关商品 */
    $sql = "SELECT g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g LEFT JOIN ". $GLOBALS['ecs']->table('category') . " AS c ON g.cat_id = c.cat_id " .
			"WHERE g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ";
	
	if ($brand)
	{//品牌
	    $sql .= " AND g.brand_id=$brand";
	}
	if ($cat)
	{//分类
	    $top = $GLOBALS['db']->getOne("SELECT parent_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat' ");
		if ($top==0)
		{//顶级分类
			$sql .= " AND c.top_id=$cat";		
		}
		else
		{
		    $row = $GLOBALS['db']->getAll("SELECT cat_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat' ");		
			if ($row)
			{//二级分类
				$child_id_arr = array_column($row, 'cat_id');
			    $sql .= " AND g.cat_id ".db_create_in($child_id_arr);
			}		
			else
			{//三级分类
				$sql .= " AND g.cat_id=$cat";												
			}
		}
	}	
//	if ($min_price>=0 && $max_price>=0)
//	{
//		$sql .= " AND g.shop_price>=$min_price";
//		$sql .= " AND g.shop_price<$max_price";		
//	}

    $sql .= " ORDER BY g.is_hot desc, g.is_new desc, g.goods_id desc";
	$sql .= " limit " .($pageNo-1)*$pageSize." ,".$pageSize;		
			
    $row = $GLOBALS['db']->getAll($sql);
	
	foreach ($row as $key => $good)
	{
		$row[$key]['level'] = '';		
		if ($good['level']>0)
		{
			$row[$key]['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['level']);
		}
		$row[$key]['intelligent'] = '';		
		if ($good['intelligent']>0)
		{
			$row[$key]['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['intelligent']);
		}
		$row[$key]['property'] = '';
		if ($good['property']>0)
		{
			$row[$key]['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$good['property']);
		}				
		//校正图片地址
		$row[$key]['goods_img'] = OSS_HOST.get_image_path($good['goods_id'],$good['goods_img'],false,'goods');		

		$row[$key]['good_count'] = 10;	
		$row[$key]['price'] = (int)$good['price'];
		
	}
	
	echo json_encode($row);	
}

/*------------------------------------------------------ */
//-- 结果筛选条件
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_condition')
{
	$cat =!empty($_REQUEST['c'])?intval($_REQUEST['c']) :0;
	$brand =!empty($_REQUEST['b'])?intval($_REQUEST['b']) :0;     

	$cat_sql = "";
	$brand_sql = "";	
	
	if ($brand)
	{//品牌
	    $sql .= " AND g.brand_id=$brand";
		
		/* 相关分类 */
	    $cat_sql = "SELECT cat_name, cat_id " .
	            "FROM " . $GLOBALS['ecs']->table('category') .
				"WHERE parent_id = 0";			
	    $cat_sql .= " ORDER BY cat_id ";
	    $row = $GLOBALS['db']->getAll($cat_sql);
//		$result['topCat'] = $row;
		foreach ($row as $k => $v)
		{
		    $cat_sql = "SELECT cat_name, cat_id " .
		            "FROM " . $GLOBALS['ecs']->table('category') .
					"WHERE parent_id = ".$v['cat_id'];			
		    $cat_sql .= " ORDER BY cat_id ";
		    $tab_cat = $GLOBALS['db']->getAll($cat_sql);
//			var_dump($tab_cat);
			if ($tab_cat)
			{
//			    $row[$k]['child_cat'] = $tab_cat;
				foreach ($tab_cat as $kk => $vv)
				{
				    $cat_sql = "SELECT c.cat_name, c.cat_id,COUNT(*) AS goods_num ".
				            " FROM " . $GLOBALS['ecs']->table('category') . "AS c, ".
				                $GLOBALS['ecs']->table('goods') . " AS g " .
				            " WHERE g.cat_id = c.cat_id " .
				            " AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
							" AND c.parent_id = ".$vv['cat_id'].            
							" AND g.brand_id=$brand ".            						
				            " GROUP BY c.cat_id HAVING goods_num > 0 ";
								
				    $cat_sql .= " ORDER BY cat_id ";
				    $child_cat = $GLOBALS['db']->getAll($cat_sql);	
					
	//				var_dump($child_cat);			
					//校正icon图标
					foreach ($child_cat as $key => $item)
					{
	//					$child_cat[$key]['icon'] = OSS_HOST.get_image_path($item['cat_id'],$item['icon'],false,'caticon');		
						unset($vv['goods_num']);
					}						
				    $tab_cat[$kk]['child_cat'] = $child_cat;					    		    
					if (!$tab_cat[$kk]['child_cat'])
					{
						unset($tab_cat[$kk]);
//						$row[$k]['child_cat'][$kk] = "";
					}
				}		
				if ($tab_cat)
				{
					$row[$k]['child_cat'] = $tab_cat;					
				}
//				var_dump($tab_cat);			
			}

		}	
//		var_dump($row);										
//		exit();
		$result['cat_list'] = $row;
	}
	if ($cat)
	{//分类
	    /* 相关品牌 */
	    $brand_sql = "SELECT b.brand_id, b.brand_name,b.brand_other_name,b.brand_short,b.brand_logo " .
	            "FROM " . $GLOBALS['ecs']->table('brand') . "AS b , " .$GLOBALS['ecs']->table('goods') . " AS g  LEFT JOIN ". $GLOBALS['ecs']->table('category') . " AS c ON g.cat_id = c.cat_id " .
				"WHERE g.brand_id = b.brand_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ";														
		//		
	    $top = $GLOBALS['db']->getOne("SELECT parent_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat' ");
		if ($top==0)
		{//顶级分类
//			$sql .= " AND c.top_id=$cat";
			$brand_sql .= " AND c.top_id=$cat";			
		}
		else
		{
		    $row = $GLOBALS['db']->getAll("SELECT cat_id FROM " . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat' ");		
			if ($row)
			{//二级分类
				$child_id_arr = array_column($row, 'cat_id');
//			    $sql .= " AND g.cat_id ".db_create_in($child_id_arr);
			    $brand_sql .= " AND g.cat_id ".db_create_in($child_id_arr);
			}		
			else
			{//三级分类
//				$sql .= " AND g.cat_id=$cat";		
				$brand_sql .= " AND g.cat_id=$cat";											
			}
		}
	}		
		
//	var_dump($sql);

    if ($brand_sql) 
    {
	    $brand_sql .= " GROUP BY g.brand_id ORDER BY b.brand_short, b.sort_order";
		
    	$brand_row = $GLOBALS['db']->getAll($brand_sql);
		
		$brand_s=array();
		foreach(range('A','Z') as $letter){ 
	   		 array_push($brand_s,$letter);
		}
	 	array_push($brand_s,"0-9");		
		
		$p ="";		
		$brand_list = array();	
		foreach ($brand_row as $key => $brand)
		{
			$brand['brand_logo'] = OSS_BAND.get_image_path($brand['brand_id'],$brand['brand_logo'],false,'brand');		
	//		$brand['brand_logo'] = OSS_HOST.get_image_path($brand['brand_id'],"");				
	
			$brand['brand_group'] = "";
			if ($brand['brand_short'] != $p){
				$brand['brand_group'] = $brand_s[$brand['brand_short']];			
			}
			$p = $brand['brand_short'];
			
			unset($brand['goods_num']);
					
			$brand_list[$key] = $brand;		
		}
			
//		var_dump($brand_list);
		$result['brand_list'] = $brand_list;
    }
			
	echo json_encode($result);	
}

else if ($_REQUEST['act'] == 'get_item')
{
		    /* 相关商品 */
    $sql = "SELECT cat_name, cat_id " .
            "FROM " . $GLOBALS['ecs']->table('category') .
			"WHERE parent_id = 0";			
    $sql .= " ORDER BY cat_id ";
    $row = $GLOBALS['db']->getAll($sql);
	$result['topCat'] = $row;
	foreach ($row as $k => $v)
	{
	    $sql = "SELECT cat_name, cat_id " .
	            "FROM " . $GLOBALS['ecs']->table('category') .
				"WHERE parent_id = ".$v['cat_id'];			
	    $sql .= " ORDER BY cat_id ";
	    $row[$k]['child_cat'] = $GLOBALS['db']->getAll($sql);
		foreach ($row[$k]['child_cat'] as $kk => $vv)
		{
		    $sql = "SELECT c.cat_name,c.icon, c.cat_id,COUNT(*) AS goods_num ".
		            " FROM " . $GLOBALS['ecs']->table('category') . "AS c, ".
		                $GLOBALS['ecs']->table('goods') . " AS g " .
		            " WHERE g.cat_id = c.cat_id " .
		            " AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
					" AND c.parent_id = ".$vv['cat_id'].            
		            " GROUP BY c.cat_id HAVING goods_num > 0 ";
						
		    $sql .= " ORDER BY cat_id ";
		    $child_cat = $GLOBALS['db']->getAll($sql);				
			//校正icon图标
			foreach ($child_cat as $key => $item)
			{
				$child_cat[$key]['icon'] = OSS_HOST.get_image_path($item['cat_id'],$item['icon'],false,'caticon');		
			}						
		    $row[$k]['child_cat'][$kk]['child_cat'] = $child_cat;					    		    
			if (!$row[$k]['child_cat'][$kk]['child_cat'])
			{
				unset($row[$k]['child_cat'][$kk]);
			}
		}	
	}	

	$result['catList'] = $row;
//	//填充跑鞋属性
//	$result['attrList'] = $GLOBALS['db']->getAll('SELECT lable_title as name,label_id as value FROM '.$GLOBALS['ecs']->table('label')." WHERE label_type = 3");
//	$result['attrShow'] = 71;	
//	//填充性别
//	$result['sexList'] = array();
//	array_push($result['sexList'],array("name"=>"不限","value"=>0));
//	array_push($result['sexList'],array("name"=>"男士","value"=>1));
//	array_push($result['sexList'],array("name"=>"女士","value"=>2));
//	array_push($result['sexList'],array("name"=>"儿童","value"=>3));	
//	//填充价格
//	$result['priceList'] = array();
//	array_push($result['priceList'],array("name"=>"不限","value"=>0));
//	array_push($result['priceList'],array("name"=>"300以下","value"=>1));
//	array_push($result['priceList'],array("name"=>"300-400","value"=>2));
//	array_push($result['priceList'],array("name"=>"400-600","value"=>3));	
//	array_push($result['priceList'],array("name"=>"600-1000","value"=>4));
//	array_push($result['priceList'],array("name"=>"1000元以上","value"=>5));	
					
	echo json_encode($result);			
}

else if ($_REQUEST['act'] == 'get_hot')
{
	/* 查询热门搜索 */
    $sql = "SELECT  keyword as name" .
            " FROM " . $GLOBALS['ecs']->table('keywords') . 
			" WHERE searchengine = 'ecshop' AND count >0 AND date = '".local_date('Y-m-d')."'".
			" ORDER BY count desc limit 4";	
	$row = $GLOBALS['db']->getAll($sql);
	
	echo json_encode($row);	
}

?>